#ifndef __CAN_DRIVER_H__
#define __CAN_DRIVER_H__

#ifdef __cplusplus
extern "C" {
#endif

#include "stm32f4xx_hal.h"
#include "stm32f4xx_hal_can.h"
#include <stdbool.h>
#include <stdint.h>
#include "FreeRTOS.h"
#include "semphr.h"
#include "task.h"
#include "queue.h"

// CAN消息结构
typedef struct {
    uint32_t id;            // 消息ID
    uint8_t data[8];        // 数据
    uint8_t len;            // 数据长度
    uint8_t format;         // 0:标准帧, 1:扩展帧
    uint8_t type;           // 0:数据帧, 1:远程帧
} can_message_t;

// CAN过滤器配置
typedef struct {
    uint32_t filter_id_high;        // 过滤器ID高16位
    uint32_t filter_id_low;         // 过滤器ID低16位
    uint32_t filter_mask_id_high;   // 过滤器掩码高16位
    uint32_t filter_mask_id_low;    // 过滤器掩码低16位
    uint32_t filter_fifo;           // FIFO分配 (0: FIFO0, 1: FIFO1)
    uint32_t filter_bank;           // 过滤器组
    uint32_t filter_mode;           // 过滤器模式
    uint32_t filter_scale;          // 过滤器尺度
    uint32_t filter_activation;     // 过滤器激活
} can_filter_config_t;

// CAN配置结构体
typedef struct {
    CAN_TypeDef *CANx;              // CAN实例
    uint32_t baudrate;              // 波特率 (kbps)
    
    // GPIO配置
    GPIO_TypeDef *tx_gpio_port;     // TX GPIO端口
    uint16_t tx_gpio_pin;           // TX GPIO引脚
    uint32_t tx_gpio_af;            // TX复用功能
    GPIO_TypeDef *rx_gpio_port;     // RX GPIO端口
    uint16_t rx_gpio_pin;           // RX GPIO引脚
    uint32_t rx_gpio_af;            // RX复用功能
    
    // FreeRTOS配置
    TaskHandle_t notify_task;       // 通知任务句柄
    uint32_t notify_mask;           // 通知掩码
    
    // 过滤器配置
    can_filter_config_t *filters;   // 过滤器数组
    uint32_t filter_count;          // 过滤器数量
    
    // 接收缓冲区配置
    uint32_t rx_buffer_size;        // 接收缓冲区大小
} can_config_t;

// CAN驱动句柄
typedef struct {
    can_config_t config;            // 配置参数
    CAN_HandleTypeDef hcan;         // HAL CAN句柄
    SemaphoreHandle_t tx_semaphore; // 发送信号量
    QueueHandle_t rx_queue;         // 接收消息队列
    bool initialized;               // 初始化标志
} can_handle_t;

// 错误码定义
typedef enum {
    CAN_DRIVER_OK = 0,
    CAN_DRIVER_ERROR,
    CAN_DRIVER_BUSY,
    CAN_DRIVER_TIMEOUT,
    CAN_DRIVER_INVALID_PARAM,
    CAN_DRIVER_NO_MESSAGE
} can_driver_status_t;

// 波特率预定义
#define CAN_BAUDRATE_125K  125000
#define CAN_BAUDRATE_250K  250000
#define CAN_BAUDRATE_500K  500000
#define CAN_BAUDRATE_1000K 1000000

// 函数声明
// CAN驱动接口
can_driver_status_t can_driver_init(can_handle_t *handle);
can_driver_status_t can_driver_deinit(can_handle_t *handle);
can_driver_status_t can_driver_send(can_handle_t *handle, const can_message_t *message, uint32_t timeout);
can_driver_status_t can_driver_receive(can_handle_t *handle, can_message_t *message, uint32_t timeout);
uint32_t can_driver_get_rx_count(can_handle_t *handle);
can_driver_status_t can_driver_set_filter(can_handle_t *handle, const can_filter_config_t *filter, uint32_t filter_index);

// 中断处理函数（需要在外部调用）
void CAN1_TX_IRQHandler(void);
void CAN1_RX0_IRQHandler(void);
void CAN1_RX1_IRQHandler(void);
void CAN1_SCE_IRQHandler(void);
void CAN2_TX_IRQHandler(void);
void CAN2_RX0_IRQHandler(void);
void CAN2_RX1_IRQHandler(void);
void CAN2_SCE_IRQHandler(void);

// 工具函数
can_driver_status_t can_driver_set_baudrate(can_handle_t *handle, uint32_t baudrate);
can_driver_status_t can_driver_set_mode(can_handle_t *handle, uint32_t mode);

// 获取默认配置
void can_driver_get_default_config(can_config_t *config, CAN_TypeDef *can_instance);
void can_driver_get_default_filter(can_filter_config_t *filter);

#ifdef __cplusplus
}
#endif

#endif /* __CAN_DRIVER_H__ */
